#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/home/utils.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
from django.conf import settings
from app.models import App
from app.constants import AppStateEnum
from saas.models import SaaSApp
from home.constants import SYS_APP_INFO
from home.models import UserSettings

def transfrom_app_to_dict(app, is_online):
    """
    将应用转换为前台展示的列表数据
    """
    logo = app.get_logo_url
    if app.is_saas:
        saas_app = SaaSApp.objects.filter(app=app)
        if saas_app.exists():
            logo = saas_app[0].get_logo_url
    app_dict = {'code': app.code,'name': app.name,
       'introduction': app.introduction,
       'logo': logo,
       'link': app.app_pro_url,
       'is_online': is_online
       }
    return app_dict


def get_user_apps(username):
    """
    按顺序获取用户桌面上展示的应用列表
    """
    user_settings, created = UserSettings.objects.get_or_create(username=username)
    apps = user_settings.apps
    app_code_list = json.loads(apps) if apps else []
    if created:
        if settings.HOST_CC:
            app_code_list.append('bk_cc')
        if settings.HOST_JOB:
            app_code_list.append('bk_job')
    user_app_list = []
    for code in app_code_list:
        if code in ('bk_cc', 'bk_job'):
            user_app_list.append(SYS_APP_INFO.get(code))
            continue
        try:
            app = App.objects.get(code=code)
            is_online = False
            if app.is_already_online and app.state not in [AppStateEnum.OUTLINE, AppStateEnum.DEVELOPMENT]:
                is_online = True
            if is_online:
                app_dict = transfrom_app_to_dict(app, is_online)
                user_app_list.append(app_dict)
        except:
            pass

    new_online_apps = App.objects.exclude(code__in=app_code_list).filter(is_already_online=True).exclude(state__in=[
     AppStateEnum.OUTLINE, AppStateEnum.DEVELOPMENT])
    for _app in new_online_apps:
        app_dict = transfrom_app_to_dict(_app, True)
        user_app_list.append(app_dict)

    user_app_code_list = [ _app.get('code') for _app in user_app_list ]
    user_settings.apps = json.dumps(user_app_code_list)
    user_settings.save()
    return user_app_list
